import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression # 逻辑回归
from sklearn.metrics import accuracy_score # 因为逻辑回归实际是解决分类问题的，因此准确度依然还可以用

def logic_ml():
    # 1- 准备数据
    cancer_df = pd.read_csv("breast-cancer-wisconsin.csv",encoding="UTF-8")

    # 2- 数据基本处理
    # 2.1- 替换空值
    cancer_df.replace("?",np.NAN, inplace=True)

    # 2.2- 删除空值
    cancer_df.dropna(inplace=True)

    # print(cancer_df)

    # 2.3- 将数据拆分得到特征数据和目标值
    # x特征舍弃掉第一列和最后一列。第一列因为是样本数据序号，无意义
    x = cancer_df.iloc[:,1:-1]
    y = cancer_df.iloc[:,-1]

    # 3- 特征工程
    # 3.1- 数据拆分得到训练集和测试集
    x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=724,stratify=y)

    # 3.2- 标准化处理
    transformer = StandardScaler()
    new_x_train = transformer.fit_transform(x_train)
    new_x_test = transformer.transform(x_test)

    # 4- 机器学习
    # 4.1- 创建逻辑回归的实例对象
    model = LogisticRegression()

    # 4.2- 训练模型
    model.fit(new_x_train,y_train)

    # 5- 模型评估
    # 5.1- 预测
    y_predict = model.predict(new_x_test)

    # 5.2- 评估
    print("预测准确度得分：",accuracy_score(y_test, y_predict))

if __name__ == '__main__':
    logic_ml()